//
//  TaskViewController.m
//  OneDoller
//
//  Created by jyj on 16/8/10.
//  Copyright © 2016年 jyj. All rights reserved.
//

#import "TaskViewController.h"
#import "PopoverViewController.h"
#import "ShopCell.h"
#import "ShopCell2.h"
#import "define.h"

@interface TaskViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UILabel *navLabel;
@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)NSArray *allKinds;
@property(nonatomic,strong)NSArray *sequence;
@property(nonatomic,strong)UITableView *tableView1;
@property(nonatomic,strong)UITableView *tableView2;

@property (nonatomic,strong) PopoverViewController *popVC;
//全部订单数组
@property(nonatomic,strong)NSMutableArray *allOrderArr;
//待处理订单数组
@property(nonatomic,strong)NSMutableArray *completeArr;
//待处理订单数组
@property(nonatomic,strong)NSMutableArray *waitDealArr;
//待安装订单数组
@property(nonatomic,strong)NSMutableArray *waitSetupArr;

@end

@implementation TaskViewController
-(NSMutableArray *)allOrderArr
{
    if (_allOrderArr==nil) {
        _allOrderArr=[NSMutableArray array];
        [_allOrderArr addObjectsFromArray:self.completeArr];
        [_allOrderArr addObjectsFromArray:self.waitDealArr];
        [_allOrderArr addObjectsFromArray:self.waitSetupArr];
    }
    return _allOrderArr;
}
-(NSMutableArray *)completeArr
{
    if (_completeArr==nil) {
        _completeArr=
        [@[@1,@2]mutableCopy];
    }
    return _completeArr;
}
-(NSMutableArray *)waitDealArr
{
    if (_waitDealArr==nil) {
        _waitDealArr=
        [@[@1,@2,@3]mutableCopy];
    }
    return _waitDealArr;
}
-(NSMutableArray *)waitSetupArr
{
    
    if (_waitSetupArr==nil) {
        _waitSetupArr=
        [@[@1,@2,@3,@4]mutableCopy];
    }
    return _waitSetupArr;
}

-(UIScrollView *)scrollView
{
    if(!_scrollView)
    {

        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 137, kScreenWidth, 495)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        CGSize size = _scrollView.frame.size;
        size.width *= 2;
        _scrollView.contentSize = size;
        _scrollView.backgroundColor = [UIColor redColor];
        _scrollView.delegate = self;
        [self.view addSubview:_scrollView];
             
    }
    
    return _scrollView;
}
-(UILabel *)navLabel
{
    if(!_navLabel)
    {
        _navLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 66, kScreenWidth/2, NAVIGATION_LINE_HEIGHT)];
        _navLabel.backgroundColor = [UIColor orangeColor];
        
        
    }
    return _navLabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // [self creatMoveView];
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    self.scrollView.delegate = self;
    [self addMessageBtns];
    [self addArrowBtns];
    [self createTableView];
    [self registerCell];
}
- (void)dealloc
{
    //  从通知中心移除自己
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//注册单元格模板
-(void)registerCell
{
    UINib *nib1=[UINib nibWithNibName:@"ShopCell" bundle:nil];
    [self.tableView1 registerNib:nib1 forCellReuseIdentifier:@"Cell"];
    self.tableView1.rowHeight = kTableViewRowHeight;
    
    UINib *nib2=[UINib nibWithNibName:@"ShopCell2" bundle:nil];
    [self.tableView2 registerNib:nib2 forCellReuseIdentifier:@"Cell2"];
    self.tableView2.rowHeight = kTableViewRowHeight;
    
   }
//创建表格
-(void)createTableView
{
    UITableView *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 492)style:UITableViewStylePlain];
    
    UITableView *tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, 492)style:UITableViewStylePlain];
    self.tableView1 = tableView1;
    self.tableView2 = tableView2;
    
    self.tableView1.delegate = self;
    self.tableView2.delegate = self;
   
    self.tableView1.dataSource = self;
    self.tableView2.dataSource = self;
    [self.scrollView addSubview:tableView1];
    [self.scrollView addSubview:tableView2];
    
}

-(void)addArrowBtns
{
    UIButton *arrowBtn1 = [self addArrowBtnX:130];
    [self.topView addSubview:arrowBtn1];

    UIButton *arrowBtn2 = [self addArrowBtnX:320];
    [self.topView addSubview:arrowBtn2];
    
    
    
}
-(UIButton *)addArrowBtnX:(CGFloat)X{
    UIButton *arrowButton = [[UIButton alloc]initWithFrame:CGRectMake(X, 20, 20 , 20)];
    
    [arrowButton setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
    
    [arrowButton addTarget:self action:@selector(chooseList:) forControlEvents:UIControlEventTouchUpInside];

    arrowButton.tag = (NSInteger) (X / (20/2));
    
      return arrowButton;
    
    
}
-(void)chooseList:(UIButton *)sender
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"PopoverViewController" bundle:nil];
    PopoverViewController *popVC = sb.instantiateInitialViewController;
    
    popVC.index = sender.tag;
    NSLog(@"%d",popVC.index);

    self.popVC = popVC;
    //蒙版
    popVC.view.frame = CGRectMake(0, 67,kScreenWidth, 100);
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2];
    bgView.frame = [UIScreen mainScreen].bounds;
    [bgView addSubview:popVC.view];
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    // 动画
    
    popVC.view.transform = CGAffineTransformMakeScale(1.0, 0.0);
    popVC.view.layer.anchorPoint = CGPointMake(0.5, 0);
    [UIView animateWithDuration:1 animations:^{
        
        popVC.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDo:)];
    [bgView addGestureRecognizer:tap];
}
//创建下拉框按钮和消息按钮
-(void)addMessageBtns
{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, TOP_VIEW_HEIGHT)];
    topView.backgroundColor = [UIColor whiteColor];
   
    UIButton *allKindsBtn = [self addMessageBtn:@"全部分类" posX:0];
    UIButton *seuenceBtn =  [self addMessageBtn:@"智能排序" posX:allKindsBtn.frame.size.width];
    [topView addSubview:allKindsBtn];
    [topView addSubview:seuenceBtn];
    [topView addSubview:self.navLabel];
    [self.view addSubview:topView];
    self.topView = topView;
    
    
    
}
-(UIButton *)addMessageBtn:(NSString *)title posX:(CGFloat)posX{
    UIButton *msgButton = [[UIButton alloc]initWithFrame:CGRectMake(posX, 0, self.view.frame.size.width / 2, TOP_VIEW_HEIGHT-NAVIGATION_LINE_HEIGHT)];
    NSAttributedString *attributeTittle = [[NSAttributedString alloc] initWithString:title attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:14] }];
    [msgButton setAttributedTitle:attributeTittle forState:UIControlStateNormal];
    msgButton.tag = (NSInteger) (posX / (self.view.frame.size.width / 2));
    self.index = msgButton.tag;
    [msgButton addTarget:self action:@selector(switchMessageDetailView:) forControlEvents:UIControlEventTouchUpInside];
    
    return msgButton;

}

-(void)switchMessageDetailView:(UIButton *)btn
{
    
    [self.scrollView setContentOffset:CGPointMake(btn.tag * self.view.frame.size.width, 0) animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  
    
    CGPoint offset = self.scrollView.contentOffset;
    CGFloat x = offset.x / 2;
    if (x > 0 && x < self.scrollView.frame.size.width) {
        CGRect frame = self.navLabel.frame;
        frame.origin.x = x;
        self.navLabel.frame = frame;
        
    }
    
    
    
}
// 新加的
- (void)tapDo:(UITapGestureRecognizer *)g
{
    
    
    
    [UIView animateWithDuration:1 animations:^{
        self.popVC.view.transform = CGAffineTransformMakeScale(1.0, 0.01);
    } completion:^(BOOL finished) {
        [self.popVC.view.superview removeFromSuperview];
    }];
}
#pragma mark1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.index==0) {
        return self.allOrderArr.count;
    }
    else if (self.index==1)
    {
        return self.waitDealArr.count;
    }
    else
    {
        return self.waitSetupArr.count;
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (tableView == self.tableView1) {
        ShopCell *shopCell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
            cell = shopCell;
    } else if (tableView == self.tableView2) {
        ShopCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
        
        cell = cell2;
    }
    return cell;
    
    
    
    
    
}


@end
